package mainpk;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Aluno implements Recordable{
	
	private int numero;
	private String nome;
	private double nota;
	
	public Aluno(int numero, String nome, double nota){
		this.numero = numero;
		this.nome = nome;
		this.nota = nota;
	}
	
	public Aluno (byte[] record) throws IOException{
		this.setBytes(record);
	}
	
	public Aluno() {
		super();
	}

	public String toString(){
				
		return this.numero + ", " + this.nome + ", " + nota;
		
	}
	
	public byte[] getBytes() throws IOException {
		
		ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();
		DataOutputStream strmDataType = new DataOutputStream(strmBytes);
		byte[] record;

		strmDataType.writeInt(this.numero);
		strmDataType.writeUTF(this.nome);
		strmDataType.writeDouble(this.nota);
			
		strmDataType.flush();
		
		record = strmBytes.toByteArray();
		
		strmBytes.close();
		strmDataType.close();
		
		return record;
	}
	
	public void setBytes(byte[] record) throws IOException {
		ByteArrayInputStream strmBytes = new ByteArrayInputStream(record);
		DataInputStream strmDataType = new DataInputStream(strmBytes);
		
		this.numero = strmDataType.readInt();
		this.nome = strmDataType.readUTF();
		this.nota = strmDataType.readDouble();
		
	}

	public String getName() {
		return this.nome;
	}

	public int getNumero() {
		return this.numero;
	}

	public double getNota() {
		return this.nota;
	}
}
